package base;

import java.util.Random;

/**
 *
 * @author Shayan
 */
public abstract class Entity {

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Entity() {
        id = createGUID();
    }
    private static Random rnd = new Random();

    protected String createGUID() {
        // Format of GUID:
        // [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}
        String uuid = new UUID((System.currentTimeMillis() ^ rnd.nextLong()) & 0xffffffffL).toString();
        StringBuffer sb = new StringBuffer();
        while (uuid.length() < 32) {
            uuid = "0" + uuid;
        }
        sb.append(uuid.substring(0, 8));
        sb.append('-');
        sb.append(uuid.substring(8, 12));
        sb.append('-');
        sb.append(uuid.substring(12, 16));
        sb.append('-');
        sb.append(uuid.substring(16, 20));
        sb.append('-');
        sb.append(uuid.substring(20));
        //System.out.println(sb.toString());
        return sb.toString();
    }

    public abstract void validate() throws ValidationException;
}
